﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;

namespace TInventory
{
    public partial class AddSizeForm : Form
    {
        List<ProductType> productTypeList;
        List<ProductSize> productSizeList;
        TIEntities dbContext;
       
        public AddSizeForm()
        {
            InitializeComponent();
            dbContext = new TIEntities();
            FillTypeCombo();
        }
        private void FillTypeCombo()
        {

            productTypeList = (from pt in dbContext.ProductTypes select pt).Distinct().ToList();
            if (productTypeList != null)
            {
                BindDataWithTypeCombo(productTypeList);
            }
            else
            {
                productTypeList = new List<ProductType>();
            }

        }
        private void BindDataWithTypeCombo(List<ProductType> productTypeList)
        {

            cmbBoxType.DataSource = null;
            cmbBoxType.DisplayMember = "Name";
            cmbBoxType.ValueMember = "ID";
            cmbBoxType.DataSource = productTypeList;
            cmbBoxType.Refresh();
        }
    
        private void BindDataWithSizeListBox(List<ProductSize> productSizeList)
        {
            listBoxSize.DataSource = null;
            listBoxSize.DisplayMember = "Name";
            listBoxSize.ValueMember = "ID";
            listBoxSize.DataSource = productSizeList;

        }

        private void btnAddNewSubType_Click_1(object sender, EventArgs e)
        {
            string sizeName = textBoxNewSize.Text.Trim();
            if (!string.IsNullOrWhiteSpace(sizeName))
            {
                ProductSize productSize;
                int selectedTypeId = Convert.ToInt32(cmbBoxType.SelectedValue.ToString());
                productSize = (from p in dbContext.ProductSizes.Include("ProductType") where p.Name == sizeName && p.ProductType.ID == selectedTypeId select p).FirstOrDefault();
                if (productSize == null)
                {
                    ProductType productType = (from pt in dbContext.ProductTypes where pt.ID == selectedTypeId select pt).FirstOrDefault();
                    productSize = new ProductSize();
                    productSize.Name = sizeName;
                    productSize.ProductType = productType;
                    productSize.ProductType.ID = selectedTypeId;
                    dbContext.AddToProductSizes(productSize);
                    dbContext.SaveChanges();

                    productSizeList.Add(productSize);

                    textBoxNewSize.Clear();
                    BindDataWithSizeListBox(productSizeList);

                }
                else
                {
                    MessageBox.Show("Type already present in database.");
                }

            }
            else
            {
                MessageBox.Show("Invalid Type Name.");
            }
        }

        private void cmbBoxType_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (cmbBoxType.SelectedIndex > -1)
            {
                int selectedTypeId = Convert.ToInt32(cmbBoxType.SelectedValue.ToString());
                productSizeList = (from pt in dbContext.ProductSizes where pt.ProductType.ID == selectedTypeId select pt).Distinct().ToList();
                if (productSizeList != null)
                {
                    BindDataWithSizeListBox(productSizeList);
                }
                else
                {
                    productSizeList = new List<ProductSize>();
                }

            }
        }
    }
}
